/*
	后台-新闻控制器
*/
'use strict';
var moment		= require('moment');
var AV 				= require('leanengine');
var mdCommon	= require('../modules/common');

var avNews 		= AV.Object.extend("New");
var avImage 	= AV.Object.extend("Image");

/**
 * 列表页面
 * 2016.04.22  by 胡斌
 */
exports.listPage = function(req, res) {
	//console.dir(req.AV.user);
	var pager,where=" objectId is exists",urlParam="",param={};
	var enabled	= req.query.enabled;
	var search	= req.query.search;

	if(typeof(enabled)!="undefined"){
		where	+= " and enabled="+enabled;
		param['enabled']=enabled;
		urlParam+="&enabled="+enabled;
	}
	if(typeof(search)!="undefined" && search.length>0){
		//console.log(search);
		search=search.replace(/(^\s*)|(\s*$)/g, "");
		if(search.length>0){
			where	+= " and (title like '%"+search+"%' or content like '%"+search+"%')";
		}
		param['search']=search;
		urlParam+="&search="+search;
	}
	//console.log(where);
	AV.Query.doCloudQuery('select count(*) from New where'+where).then(function(data){
		//console.dir(data);
		pager		= mdCommon.Paginate(req.query.page, req.query.pagesize, data.count);
		return AV.Query.doCloudQuery('select include user,* from New where'+where+' limit '+(pager.currentPage-1)*pager.pagesize+','+pager.pagesize+" order by createdAt desc");
	}).then(function(data){
		res.render('admin-news-list',{
			user:req.AV.user,
			results:data.results,
			moment:moment,
			enabled:enabled,
			pager:pager,
			urlParam:urlParam,
			param:param
		});
	},function(error) {
		mdCommon.sentErr(res,req,error);
	});
}

/**
 * 文章详情页面
 * 2016.05.03  by 胡斌
 */
exports.detailPage = function(req, res) {
	//console.log("detailPage:"+req.AV.user);
	var id					= req.query.id;
	var queryNews 	= new AV.Query(avNews);
	if(!id)id="-1";

	queryNews.equalTo("objectId",id);
	queryNews.first().then(function(result) {
		if(typeof(result)=="undefined")result=false;
		res.render('admin-news-detail.html',{
			user:req.AV.user,
			result:result,
			moment:moment
		});
	},function(error) {
		mdCommon.sentErr(res,req,error);
	});
}

/**
 * 文章管理-保存
 * 2015.05.24  by 胡斌
 */
exports.doSave = function(req, res) {
	//console.log("doSave");
	var id					= req.body.id;
	var enabled			= false;
	var queryNews 	= new AV.Query(avNews);
	if(typeof(id)=="undefined")id=-1;
	if(req.body.enabled==1)enabled			= true;

//	console.log("id:"+id);
	queryNews.equalTo("objectId",id);
	queryNews.first().then(function(News) {
		if(typeof(News)=="undefined"){
			News	= new avNews();
		}
		var user 		= req.AV.user;

		News.set("content",req.body.content);
		News.set("title",req.body.title);
		News.set("user",user);
		News.set("enabled",enabled);
		if(req.body.cover){
			var cover		= new avImage();
			cover.id		= req.body.cover;
			News.set("cover",cover);
		}
		return News.save();
	}).then(function(result) {
	//	console.dir(result);
		if(req.body.ajax){
			res.json({result:result});
		}else{
			res.redirect('/admin/News/list');
		}
	},function(error){
		console.dir(error);
		mdCommon.sentErr(res,req,error);
	});
}

/**
 * 文章管理-删除
 * 2015.05.02  by 胡斌
 */
exports.doDelete = function(req, res) {
	var ids					= req.body.ids?req.body.ids:req.body.id;
	var queryNews 		= new AV.Query(avNews);
	if(typeof(ids)=="undefined")ids="-1";
	var idArr				= ids.split(",");

 console.log(ids);

	queryNews.equalTo("objectId",ids);
	queryNews.destroyAll().then(function(result) {
		if(req.body.ajax){
			res.json({success:true});
		}else{
			res.redirect('/admin/News/list');
		}
    },function(error) {
		mdCommon.sentErr(res,req,error);
	});
}
